﻿Imports System.Data.OleDb

Public Class DAOLop
    Public Sub DAOLop()

    End Sub
    Public Function Connection(ByVal path As String) As OleDbConnection
        Return New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path)
    End Function
    Public Function LoadAllData(ByVal path As String) As DataSet
        Dim dset As DataSet = New DataSet
        Dim strQuery As String = "Select * from Lop"
        Dim conn As OleDbConnection = Me.Connection(path)
        conn.Open()
        Dim cmd As OleDbCommand = New OleDbCommand(strQuery, conn)
        Dim ad As OleDbDataAdapter = New OleDbDataAdapter(cmd)
        conn.Close()
        ad.Fill(dset, "Lop")
        Return dset
    End Function
    Public Function LoadTable(ByVal strQuery As String, ByVal path As String) As DataTable

        Dim dset As DataSet = New DataSet
        Dim conn As OleDbConnection = Me.Connection(path)
        conn.Open()
        Dim cmd As OleDbCommand = New OleDbCommand(strQuery, conn)
        Dim ad As OleDbDataAdapter = New OleDbDataAdapter(cmd)
        conn.Close()
        ad.Fill(dset, "Lop")
        Return dset.Tables("Lop")
    End Function
    Public Function Insert(ByVal Lop As DTOLop, ByVal path As String) As Boolean
        Dim strQuery As String = "Insert into Lop(TenLop,SiSo) values('" + Lop.TenLop.ToString() + "'," + Lop.SiSo.ToString() + ")"
        Dim conn As OleDbConnection = Me.Connection(path)
        conn.Open()
        Dim cmd As OleDbCommand = New OleDbCommand(strQuery, conn)
        Dim flag As Boolean = False
        Try
            flag = cmd.ExecuteNonQuery() > 0
        Catch ex As Exception
            flag = False
        End Try

        conn.Close()
        Return flag
    End Function
    Public Function Update(ByVal lop As DTOLop, ByVal path As String) As Boolean
        Dim strQuery As String = "Upadate Lop set SiSo=" + lop.SiSo.ToString() + "where TenLop='" + lop.TenLop.ToString() + "'"
        Dim conn As OleDbConnection = Me.Connection(path)
        conn.Open()
        Dim cmd As OleDbCommand = New OleDbCommand(strQuery, conn)
        Dim flag As Boolean = False
        Try
            flag = cmd.ExecuteNonQuery() > 0
        Catch ex As Exception
            flag = False
        End Try

        conn.Close()
        Return flag
    End Function

    Public Function Delete(ByVal ID As String, ByVal path As String) As Boolean
        Dim strQuery As String = "Delete * from Lop where TenLop='" + ID + "'"
        Dim conn As OleDbConnection = Me.Connection(path)
        conn.Open()
        Dim cmd As OleDbCommand = New OleDbCommand(strQuery, conn)
        Dim flag As Boolean = False
        Try
            flag = cmd.ExecuteNonQuery() > 0
        Catch ex As Exception
            flag = False
        End Try

        conn.Close()
        Return flag
    End Function
End Class
